import { createStore, Store } from 'vuex';
import { InjectionKey } from 'vue';

// 定义 store1 的状态类型
interface userInfoState {
	username : string;
	token : string;
}

// 定义 store1 的注入键
export const userKey : InjectionKey<Store<userInfoState>> = Symbol();

// 创建 store1 实例
export const userInfoStore = createStore<userInfoState>({
	state: {
		username: '-',
		token: '-'
	},
	mutations: {
		setUserinfo(state, info : userInfoState) {
			state.username = info.username;
			state.token = info.token;
			uni.setStorageSync('token',state.token);
		},
		clearUserInfo(state) {
			state.username = '';
			state.token = '';
			uni.removeStorageSync('token');
		}
	},
	actions: {
		serUserAsync({ commit }, info : userInfoState) {
			setTimeout(() => {
				commit('setUserinfo', info);
			}, 1000);
		}
	},
	getters: {
		getUserInfo(state) {
			const user : userInfoState = {
				username: state.username,
				token: state.token
			};
			return user;
		}
	}
});